/*
 * Copyright 2013 Aliyun.com All right reserved. This software is the
 * confidential and proprietary information of Aliyun.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Aliyun.com .
 */
package com.mtauto.service;

import java.util.Collection;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.lang.ArrayUtils;

import com.mtauto.model.Device;

/**
 * @author <a href="mailto:li.jinl@alibaba-inc.com">Stone.J</a> Nov 19, 2013
 */
public class DeviceService {

    private Map<String, Device> devices = new ConcurrentHashMap<String, Device>();

    private AdbService          adbService;

    private Timer               timer   = new Timer();
    private long                period  = 10 * 60 * 1000;                         // default value: 10 minutes

    public void init() {
        timer.scheduleAtFixedRate(new TimerTask() {

            @Override
            public void run() {
                getDevices(true);
            }
        }, 0, period);
    }

    public void destroy() {
        timer.cancel();
    }

    public Collection<Device> getDevices() {
        return getDevices(false);
    }

    public Collection<Device> getDevices(boolean force) {
        if (devices.isEmpty() || force) {
            String[] serialnos = adbService.getSerialnos();
            for (String serialno : serialnos) {
                getDevice(serialno, true);
            }
        }
        return devices.values();
    }

    public Device getDevice(String serialno) {
        return getDevice(serialno, false);
    }

    public Device getDevice(String serialno, boolean force) {
        if (devices.containsKey(serialno) && !force) {
            return devices.get(serialno);
        }
        String[] serialnos = adbService.getSerialnos();
        if (!ArrayUtils.contains(serialnos, serialno)) {
            devices.remove(serialno);
            return null;
        }
        Device dev = new Device();
        dev.setSerialno(serialno);
        dev.setProduct(adbService.getProduct(serialno));
        dev.setDisplay(adbService.getDisplay(serialno));
        dev.setNetinfo(adbService.getNetinfo(serialno));
        devices.put(serialno, dev);
        return dev;
    }

    public void setAdbService(AdbService adbService) {
        this.adbService = adbService;
    }

    public void setPeriod(long period) {
        this.period = period;
    }

    public static void main(String[] args) throws Exception {
        AdbService adbService = new AdbService();
        adbService.setAdb("/Users/stone/local/installed/adt-bundle-mac-x86_64/sdk/platform-tools/adb");
        DeviceService deviceService = new DeviceService();
        deviceService.setAdbService(adbService);

        deviceService.init();
        Thread.sleep(11000);
        deviceService.destroy();

        //Device dev = deviceService.getDevice("7D45HQLZDA5DCQOZ", true);
        //System.out.println(dev);

        //Collection<Device> devs = deviceService.getDevices(true);
        //System.out.println(devs);

    }

}
